﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OracleClient;
using System.Net.Mail;
using System.Net;
using System.Web.Script.Serialization;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest(HttpContext context)
    {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {

            //获取调拨申请列表数据
            case "get_request_list":
                get_request_list(context);
                break;
            case "getlist_new":
                getlistnew(context);
                break;
            case "getlist_mx":
                getlist_mx(context);
                break;
            //根据经销商编号获取地址联系人电话
            case "get_address_bycode":
                get_address_bycode(context);
                break;
            //验证条码
            case "get_tm":
                gettm(context);
                break;
            //提交和保存
            case "requestsaveandtj":
                requestsaveandtj(context);
                break;
            //根据id获取明细
            case "getinfo_byids":
                getinfo_byids(context);
                break;
            //修改
            case "editbydocno":
                editbydocno(context);
                break;
            //作废
            case "delete":
                delete(context);
                break;
            //重新发布
            case "againfabu":
                againfabu(context);
                break;
            //取消未抢单
            case "deletewqd":
                deletewqd(context);
                break;
            //处理进度
            case "get_speed_list":
                get_speed_list(context);
                break;
            //获取申请货品详情
            case "get_product_list":
                get_product_list(context);
                break;
            //获取进度货品详情
            case "get_speedproduct_list":
                get_speedproduct_list(context);
                break;
            //获取相应渠道店铺列表
            case "searchname":
                getinfo_dpinfo(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }
    //获取店铺详细信息
    public void getinfo_dpinfo(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string sqlname = "exec [dbo].[zdy_获取经销商下店铺] '{0}'";
        sqlname = string.Format(sqlname,customerCode);
        DataTable dtname  = DBHelper.ExecuteDataTable(sqlname);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtname.Rows.Count; i++)
        {
            sb.Append("{\"code\":\"" + dtname.Rows[i]["code"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtname.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"address2\":\"" + dtname.Rows[i]["address2"].ToString() + "\",");
            sb.Append("\"contacter\":\"" + dtname.Rows[i]["contacter"].ToString() + "\",");
            sb.Append("\"phone\":\"" + dtname.Rows[i]["phone"].ToString() + "\"}");
            if (i < dtname.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取进度货品详情
    public void get_speedproduct_list(HttpContext context)
    {
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);

        string sqllist = @" select a.docno,b.name,b.yscode+b.ysname ys,b.cmwm,b.value,b.nf,b.jj,b.price,
 'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,a.sl FROM
  t_transferDeliver_mx a left join t_product b on a.tmid=b.id
 where a.docno='{0}'";
        sqllist = string.Format(sqllist,docnos);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"ys\":\"" + dtResult.Rows[i]["ys"].ToString() + "\",");
            sb.Append("\"cmwm\":\"" + dtResult.Rows[i]["cmwm"].ToString() + "\",");
            sb.Append("\"value\":\"" + dtResult.Rows[i]["value"].ToString() + "\",");
            sb.Append("\"nf\":\"" + dtResult.Rows[i]["nf"].ToString() + "\",");
            sb.Append("\"jj\":\"" + dtResult.Rows[i]["jj"].ToString() + "\",");
            sb.Append("\"price\":\"" + dtResult.Rows[i]["price"].ToString() + "\",");
            sb.Append("\"imgurl\":\"" + dtResult.Rows[i]["imgurl"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtResult.Rows[i]["sl"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取申请货品详情
    public void get_product_list(HttpContext context)
    {
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);

        string sqllist = @"
 select a.docno,a.name,a.ys,a.tmid,a.cmwm,a.value,a.nf,a.jj,a.price,a.imgurl,a.sqsl,isnull(b.qdsl,0) qdsl
  from (select a.docno,b.name,b.yscode+b.ysname ys,a.tmid,b.cmwm,b.value,b.nf,b.jj,b.price,
 'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,sum(a.sl) sqsl FROM
  t_transferRequest_mx a left join t_product b on a.tmid=b.id
 where a.docno='{0}'
 group by a.docno,b.name,b.yscode,b.ysname,a.tmid,b.cmwm,b.value,b.nf,b.jj,b.price,b.imageurl)a
 left join  (select requestdocno,tmid,isnull(sum(sl),0) as qdsl from [t_transferDeliver_mx] where docno in(
  select docno from [dbo].[t_transferDeliver_main] where requestdocno='{0}')  
  group by requestdocno,tmid
 )b on a.docno=b.requestdocno and a.tmid=b.tmid";
        sqllist = string.Format(sqllist,docnos);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"ys\":\"" + dtResult.Rows[i]["ys"].ToString() + "\",");
            sb.Append("\"cmwm\":\"" + dtResult.Rows[i]["cmwm"].ToString() + "\",");
            sb.Append("\"value\":\"" + dtResult.Rows[i]["value"].ToString() + "\",");
            sb.Append("\"nf\":\"" + dtResult.Rows[i]["nf"].ToString() + "\",");
            sb.Append("\"jj\":\"" + dtResult.Rows[i]["jj"].ToString() + "\",");
            sb.Append("\"price\":\"" + dtResult.Rows[i]["price"].ToString() + "\",");
            sb.Append("\"imgurl\":\"" + dtResult.Rows[i]["imgurl"].ToString() + "\",");
            sb.Append("\"sqsl\":\"" + dtResult.Rows[i]["sqsl"].ToString() + "\",");
            sb.Append("\"qdsl\":\"" + dtResult.Rows[i]["qdsl"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取处理进度列表
    public void get_speed_list(HttpContext context)
    {
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);

        string sqllist = @" select a.docno,b.username,case a.status when 0  then '待发货' when 90
                          then '生成伯俊发货单失败' when 99 then '完成' else '' end zt,a.bjmsg
                          ,sum(c.sl) sl,a.createtime from [dbo].t_transferDeliver_main a 
                         left join sys_users b on a.sale_mobile=b.phone
                         left join t_transferDeliver_mx c on a.docno=c.docno
                          where a.requestdocno='{0}' and ltrim(rtrim(b.isactive))='Y' 
                          group by a.docno,b.username,a.status,a.createtime,a.bjmsg
                          order by a.createtime desc";
        sqllist = string.Format(sqllist,docnos);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"username\":\"" + dtResult.Rows[i]["username"].ToString() + "\",");
            sb.Append("\"zt\":\"" + dtResult.Rows[i]["zt"].ToString() + "\",");
            sb.Append("\"bjmsg\":\"" + dtResult.Rows[i]["bjmsg"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtResult.Rows[i]["sl"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "exec [dbo].[zdy_取消申请单] {0}";
        sqldelete = string.Format(sqldelete, getids);
        DataTable dtdelete= DBHelper.ExecuteDataTable(sqldelete);
        if (dtdelete.Rows.Count > 0)
        {
            flag = true;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        else
        {
            flag = false;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //重新发布
    public void againfabu(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "exec [zdy_重新发布抢单] {0}";
        sqldelete = string.Format(sqldelete, getids);
        DataTable dtdelete= DBHelper.ExecuteDataTable(sqldelete);
        if (dtdelete.Rows.Count > 0)
        {
            flag = true;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        else
        {
            flag = false;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //取消未抢单
    public void deletewqd(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "exec [zdy_取消未抢单的] {0}";
        sqldelete = string.Format(sqldelete, getids);
        DataTable dtdelete= DBHelper.ExecuteDataTable(sqldelete);
        if (dtdelete.Rows.Count > 0)
        {
            flag = true;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        else
        {
            flag = false;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取修改明细
    public void getinfo_byids(HttpContext context)
    {
        string ids = PageBase.ext_string(context.Request.Params["ids"]);
        //主表
        string sqlmain = @"select a.id,a.docno,a.mobile,a.request_storecode,b.username,a.takeman,
         a.takephone,replace(replace(REPLACE(a.takeaddress,char(10),''),CHAR(13),''),CHAR(32),'')  takeaddress,
         replace(replace(REPLACE(a.remark,char(10),''),CHAR(13),''),CHAR(32),'')  remark from [t_transferRequest_main] a
         left join [sys_users] b on a.request_storecode=b.loginid and a.mobile=b.phone
         where a.id='{0}' and ltrim(rtrim(a.isactive))='Y' ";
        sqlmain = string.Format(sqlmain,ids);
        DataTable dtmain = DBHelper.ExecuteDataTable(sqlmain);
        //明细表
        string sqlmx = @"select a.id,b.no,b.name+b.yscode+b.cmwm as wmno,a.tmid,a.sl from [t_transferRequest_mx] a left join t_product b
         on a.tmid=b.id  where docno='{0}' order by a.id ";
        sqlmx = string.Format(sqlmx,dtmain.Rows[0]["docno"].ToString());
        DataTable dtmx = DBHelper.ExecuteDataTable(sqlmx);

        sb = new StringBuilder();
        sb.Append("{\"itemMain\":[");
        for (int i = 0; i < dtmain.Rows.Count; i++)
        {
            string storename = dtmain.Rows[i]["request_storecode"].ToString() + "[" + dtmain.Rows[i]["username"].ToString() +"]";
            sb.Append("{\"id\":\"" + dtmain.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"storename\":\"" + storename + "\",");
            sb.Append("\"docno\":\"" + dtmain.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"mobile\":\"" + dtmain.Rows[i]["mobile"].ToString() + "\",");
            sb.Append("\"request_storecode\":\"" + dtmain.Rows[i]["request_storecode"].ToString() + "\",");
            sb.Append("\"takeman\":\"" + dtmain.Rows[i]["takeman"].ToString() + "\",");
            sb.Append("\"takephone\":\"" + dtmain.Rows[i]["takephone"].ToString() + "\",");
            sb.Append("\"takeaddress\":\"" + dtmain.Rows[i]["takeaddress"].ToString() + "\",");
            sb.Append("\"remark\":\"" + dtmain.Rows[i]["remark"].ToString().Replace(',', '，').Replace('{', '(').Replace('}', ')').Replace('[', '(').Replace(']', ')').Replace('"', ' ').Replace('：', ':') + "\"}");
            if (i < dtmain.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("],");
        sb.Append("\"itemMx\":[");
        for (int j = 0; j < dtmx.Rows.Count; j++)
        {
            sb.Append("{\"id\":\"" + dtmx.Rows[j]["id"].ToString() + "\",");
            sb.Append("\"no\":\"" + dtmx.Rows[j]["no"].ToString() + "\",");
            sb.Append("\"wmno\":\"" + dtmx.Rows[j]["wmno"].ToString() + "\",");
            sb.Append("\"tmid\":\"" + dtmx.Rows[j]["tmid"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtmx.Rows[j]["sl"].ToString() + "\"}");
            if (j < dtmx.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void editbydocno(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag=false;
        string lx = Utility.Trim(context.Request.Params["lxs"]); //类型
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string id = Utility.Trim(list.ArrayObj_main[0].ID);
            string docno = Utility.Trim(list.ArrayObj_main[0].DOCNO);
            string shr = Utility.Trim(list.ArrayObj_main[0].SHR);
            string phone = Utility.Trim(list.ArrayObj_main[0].PHONE);
            string address = Utility.Trim(list.ArrayObj_main[0].ADDRESS);
            string remark = Utility.Trim(list.ArrayObj_main[0].REMARK);
            string customercode = Utility.Trim(list.ArrayObj_main[0].CUSTOMERCODE);
            int status = 0;
            if (lx == "save")
            {
                flag = true;
                status = 0;
            }
            else{
                string sqlye = "exec [zdy_查询经销商余额] '{0}'";
                sqlye = string.Format(sqlye, customercode);
                DataTable dtye = DBHelper.ExecuteDataTable(sqlye);
                if (dtye.Rows.Count > 0)
                {
                    if (Utility.ToDecimal(dtye.Rows[0]["FEECANTAKE"].ToString()) > 0)
                    {
                        status = 1;
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                        msg = "当前可提货金额为:"+dtye.Rows[0]["FEECANTAKE"].ToString()+"，无法发布，请先保存！";
                    }
                }
                else
                {
                    flag = false;
                    msg = "未查到该经销商余额，请联系管理员！";
                }
            }
            if (flag == true)
            {
                //主体信息
                sqlsave = @"update [t_transferRequest_main] set
                            [takeman]='{0}'
                           ,[takephone]='{1}'
                           ,[takeaddress]='{2}'
                           ,[remark]='{3}'
                           ,[status]={4}
                           ,[updatetime]='{5}'
                           where id={6};";
                sqlsave = string.Format(sqlsave, shr, phone, address, remark, status, DateTime.Now.ToString(), id);
                if (list.ArrayObj_mx.Count > 0)
                {
                    DBHelper.ExecuteNonQuery("delete from t_transferRequest_mx where docno='" + docno + "'");
                    //明细
                    for (int i = 0; i < list.ArrayObj_mx.Count; i++)
                    {
                        sqlsave += @"insert into t_transferRequest_mx(  docno, tmid, sl, isactive, createtime) values
                    ('{0}','{1}','{2}','{3}','{4}');";
                        string tmid = Utility.Trim(list.ArrayObj_mx[i].TMID);
                        Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
                        sqlsave = string.Format(sqlsave, docno, tmid, sl, 'Y', DateTime.Now.ToString());
                    }
                }
                //插入数据库条数
                int zxcount = 0;
                try
                {
                    zxcount = DBHelper.ExecuteNonQuery(sqlsave);
                }
                catch (Exception e)
                {
                    flag = false;
                    msg = e.Message;
                }
                //计算前台传过来的数据条数
                int zqts = list.ArrayObj_main.Count + list.ArrayObj_mx.Count;
                if (zxcount == zqts)
                {
                    if (lx == "save")
                    {
                        flag = true;
                        msg = "保存成功！";
                    }
                    else
                    {
                        flag = true;
                        msg = "发布成功，等待系统处理！";
                        string sqlfb = "exec [zdy_发布抢单] '{0}'";
                        sqlfb = string.Format(sqlfb, docno);
                        DataTable dtfb = DBHelper.ExecuteDataTable(sqlfb);
                    }
                }
                else
                {
                    if (lx == "save")
                    {
                        flag = false;
                        msg = "保存失败，请联系管理员！";
                    }
                    else
                    {
                        flag = false;
                        msg = "发布失败，请联系管理员！";
                    }
                }
            }
        }
        else
        {
            flag = false;
            msg = "失败，请联系管理员！";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //调拨申请保存或者发布
    public void requestsaveandtj(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag=false;
        string djbhs = PageBase.ext_string(context.Request.Params["djbhs"]);
        if (djbhs != "")
        {
            string del = @"delete from [t_transferRequest_main] where docno='{0}';delete from [t_transferRequest_mx] where docno='{0}';";
            del = string.Format(del, djbhs);
            DBHelper.ExecuteNonQuery(del);
        }
        //生成单号
        string sqldjbh = @"declare @BH INT;declare @MID varchar(50);SELECT @BH=ISNULL(MAX(RIGHT(docno,4)),0)+1 FROM 
                         t_transferRequest_main WHERE LEFT(docno,2)='TF' AND  RIGHT(LEFT(docno,8),6)=CONVERT(VARCHAR(10),GETDATE(),12);
                         set @MID='TF'+CONVERT(VARCHAR(10),GETDATE(),12)+CAST(right((10000+@BH),4) AS VARCHAR(20)) ;
                         SELECT @MID";
        DataTable dtResult = DBHelper.ExecuteDataTable(sqldjbh);
        string djbh = dtResult.Rows[0][0].ToString();
        string lx = Utility.Trim(context.Request.Params["lxs"]); //类型

        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string customercode = Utility.Trim(list.ArrayObj_main[0].CUSTOMERCODE);
            string shr = Utility.Trim(list.ArrayObj_main[0].SHR);
            string phone = Utility.Trim(list.ArrayObj_main[0].PHONE);
            string address = Utility.Trim(list.ArrayObj_main[0].ADDRESS);
            string remark = Utility.Trim(list.ArrayObj_main[0].REMARK);
            string mobile = Utility.Trim(list.ArrayObj_main[0].MOBILE);
            int status = 0;
            if (lx == "save")
            {
                flag = true;
                status = 0;
            }

            else{
                string sqlye = "exec [zdy_查询经销商余额] '{0}'";
                sqlye = string.Format(sqlye, customercode);
                DataTable dtye = DBHelper.ExecuteDataTable(sqlye);
                if (dtye.Rows.Count > 0)
                {
                    if (Utility.ToDecimal(dtye.Rows[0]["FEECANTAKE"].ToString()) > 0)
                    {
                        status = 1;
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                        msg = "当前可提货金额不足，无法发布，请先保存！";
                    }
                }
                else
                {
                    flag = false;
                    msg = "未查到该经销商余额，请联系管理员！";
                }
            }
            if (flag == true)
            {
                //主体信息
                sqlsave = @"INSERT INTO [t_transferRequest_main]( docno, request_storecode, takeman, takephone, takeaddress, kdlx, 
                                 iscf, remark, status, isactive, creater, createtime, updater, updatetime,mobile)
                        VALUES
                       ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}');";
                sqlsave = string.Format(sqlsave, djbh, customercode, shr, phone, address, 0, 0, remark, status, 'Y', customercode, DateTime.Now.ToString(), customercode,
                                       DateTime.Now.ToString(), mobile);
                //明细
                for (int i = 0; i < list.ArrayObj_mx.Count; i++)
                {
                    sqlsave += @"insert into t_transferRequest_mx(docno, tmid, sl, isactive, createtime) values
                    ('{0}','{1}','{2}','{3}','{4}');";
                    string tmid = Utility.Trim(list.ArrayObj_mx[i].TMID);
                    Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
                    sqlsave = string.Format(sqlsave, djbh, tmid, sl, 'Y', DateTime.Now.ToString());
                }
                //插入数据库条数
                int zxcount = 0;
                try
                {
                    zxcount = DBHelper.ExecuteNonQuery(sqlsave);
                }
                catch (Exception e)
                {
                    flag = false;
                    msg = e.Message;
                }
                //计算前台传过来的数据条数
                int zqts = list.ArrayObj_main.Count + list.ArrayObj_mx.Count;
                if (zxcount == zqts)
                {
                    if (lx == "save")
                    {
                        flag = true;
                        msg = "保存成功！";
                    }
                    else
                    {
                        flag = true;
                        msg = "发布成功，等待系统处理！";
                        string sqlfb = "exec [zdy_发布抢单] '{0}'";
                        sqlfb = string.Format(sqlfb, djbh);
                        DataTable dtfb = DBHelper.ExecuteDataTable(sqlfb);
                    }
                }
                else
                {
                    if (lx == "save")
                    {
                        flag = false;
                        msg = "保存失败，请联系管理员！";
                    }
                    else
                    {
                        flag = false;
                        msg = "发布失败，请联系管理员！";
                    }
                }
            }
        }
        else
        {
            flag = false;
            msg = "失败，请联系管理员！";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\",\"djbh\":\"" + djbh + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //验证条码
    public void gettm(HttpContext context)
    {
        string tm = Utility.Trim(context.Request.Params["tms"]); //货号
        string customerCode = Utility.Trim(context.Request.Params["customerCodes"]); //经销商编号
        try
        {
            string sqlview = "exec [dbo].[zdy_查询条码信息] '{0}'";
            sqlview = string.Format(sqlview,tm);
            dt = DBHelper.ExecuteDataTable(sqlview) ;
            if (dt.Rows[0]["remsg"].ToString()=="成功")
            {
                //string sql_ckfh = "exec [dbo].[zdy_查询是否发过货] '{0}','{1}'";
                string sql_ckfh = "declare @sl int;exec [zdy_查询是否发过货] '{0}','{1}',@sl output;select @sl as sl;";
                sql_ckfh = string.Format(sql_ckfh,customerCode,dt.Rows[0]["tmid"].ToString());
                DataTable dt_ckfh = DBHelper.ExecuteDataTable(sql_ckfh);
                if(Utility.ToInt(dt_ckfh.Rows[0]["sl"].ToString()) > 0)
                {
                    context.Response.Write("{\"flag\":true,\"msg\":\"" + dt.Rows[0]["remsg"].ToString() + "\",\"tmid\":\"" + dt.Rows[0]["tmid"].ToString() + "\",\"bjtm\":\"" + dt.Rows[0]["bjtm"].ToString() + "\",\"bjwm\":\"" + dt.Rows[0]["bjwm"].ToString() + "\"}");
                }
                else
                {
                    string remsg = "总部未发过此货，不能申请调拨";
                    context.Response.Write("{\"flag\":false,\"msg\":\"" +remsg + "\"}");
                }

            }
            else
            {
                context.Response.Write("{\"flag\":false,\"msg\":\"" + dt.Rows[0]["remsg"].ToString() + "\"}");
            }
        }
        catch (Exception ex)
        {
            context.Response.Write("{\"flag\":false,\"msg\":\"" + ex + "\"}");
        }
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //根据经销商编号获取地址联系人电话
    public void get_address_bycode(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string mobile = PageBase.ext_string(context.Request.Params["mobiles"]);
        string sqllist = @"select a.loginid,a.username,b.contacter,b.phone,b.storeaddress from sys_users a 
left join [t_addressList] b on a.phone=b.mobile
where ltrim(rtrim(a.isactive))='Y' and  ltrim(rtrim(b.isdefault))='Y' and a.phone='{0}'";
        sqllist = string.Format(sqllist,mobile);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"loginid\":\"" + dtResult.Rows[i]["loginid"].ToString() + "\",");
            sb.Append("\"username\":\"" + dtResult.Rows[i]["username"].ToString() + "\",");
            sb.Append("\"contacter\":\"" + dtResult.Rows[i]["contacter"].ToString() + "\",");
            sb.Append("\"phone\":\"" + dtResult.Rows[i]["phone"].ToString() + "\",");
            sb.Append("\"storeaddress\":\"" + dtResult.Rows[i]["storeaddress"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //获取商品信息new
    public void getlistnew(HttpContext context)
    {

        string sqllist = @"select id,ltlx,ltxh,hw,Convert(decimal(18,0),price) price,rtrim(ltrim(isred)) isred,
createtime,updatetime,rtrim(ltrim(isactive)) isactive from m_product where rtrim(ltrim(isactive))='Y' order by xh";
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"ltlx\":\"" + dtResult.Rows[i]["ltlx"].ToString() + "\",");
            sb.Append("\"ltxh\":\"" + dtResult.Rows[i]["ltxh"].ToString() + "\",");
            sb.Append("\"hw\":\"" + dtResult.Rows[i]["hw"].ToString() + "\",");
            sb.Append("\"price\":\"" + dtResult.Rows[i]["price"].ToString() + "\",");
            sb.Append("\"isred\":\"" + dtResult.Rows[i]["isred"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\",");
            sb.Append("\"updatetime\":\"" + dtResult.Rows[i]["updatetime"].ToString() + "\",");
            sb.Append("\"isactive\":\"" + dtResult.Rows[i]["isactive"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取商品信息
    public void get_request_list(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string mobile= PageBase.ext_string(context.Request.Params["mobiles"]);

        string  sqllist = string.Format("exec [zdy_查询调拨申请列表] '{0}'",mobile);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"mobile\":\"" + dtResult.Rows[i]["mobile"].ToString() + "\",");
            sb.Append("\"username\":\"" + dtResult.Rows[i]["username"].ToString() + "\",");
            sb.Append("\"request_storecode\":\"" + dtResult.Rows[i]["request_storecode"].ToString() + "\",");
            sb.Append("\"takeman\":\"" + dtResult.Rows[i]["takeman"].ToString() + "\",");
            sb.Append("\"takephone\":\"" + dtResult.Rows[i]["takephone"].ToString() + "\",");
            sb.Append("\"takeaddress\":\"" + dtResult.Rows[i]["takeaddress"].ToString()+ "\",");
            sb.Append("\"zt\":\"" + dtResult.Rows[i]["zt"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtResult.Rows[i]["sl"].ToString() + "\",");
            sb.Append("\"qdsl\":\"" + dtResult.Rows[i]["qdsl"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取商品详细信息
    public void getlist_mx(HttpContext context)
    {
        string id = PageBase.ext_string(context.Request.Params["ids"]);
        string sqllist = @"select a.id,a.name,a.reprice,a.buyprice,a.pfprice,a.huohao,
a.remark,a.createtime,a.qckc,isnull(a.qckc,0)+isnull(b.cgsl,0)-isnull(c.xssl,0) kc
,isnull(b.cgsl,0) cgsl,isnull(c.xssl,0) xssl from product a 
left join (select productid,sum(sl) cgsl from purchase_item  where isactive='Y'  group by productid)b on a.id=b.productid
left join (select productid,sum(sl) xssl from sale_item  where isactive='Y'  group by productid)c on a.id=c.productid
where a.id={0} and a.isactive='Y'
group by a.id,a.name,a.qckc,a.reprice,a.buyprice,a.huohao,a.remark,a.createtime,c.xssl,b.cgsl,a.pfprice
order by a.createtime desc";
        sqllist = string.Format(sqllist,id);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"reprice\":\"" + dtResult.Rows[i]["reprice"].ToString() + "\",");
            sb.Append("\"buyprice\":\"" + dtResult.Rows[i]["buyprice"].ToString() + "\",");
            sb.Append("\"pfprice\":\"" + dtResult.Rows[i]["pfprice"].ToString() + "\",");
            sb.Append("\"qckc\":\"" + dtResult.Rows[i]["qckc"].ToString() + "\",");
            sb.Append("\"kc\":\"" + dtResult.Rows[i]["kc"].ToString() + "\",");
            sb.Append("\"cgsl\":\"" + dtResult.Rows[i]["cgsl"].ToString() + "\",");
            sb.Append("\"xssl\":\"" + dtResult.Rows[i]["xssl"].ToString() + "\",");
            sb.Append("\"huohao\":\"" + dtResult.Rows[i]["huohao"].ToString() + "\",");
            sb.Append("\"remark\":\"" + dtResult.Rows[i]["remark"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<main> ArrayObj_main { get; set; }//主表信息
        public List<mx> ArrayObj_mx { get; set; }//明细信息
    }
    public class main
    {
        //单据编号
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //单据编号
        public string docno;
        public string DOCNO
        {
            get { return docno; }
            set { docno = value; }
        }
        //店仓编号
        public string userid;
        public string USERID
        {
            get { return userid; }
            set { userid = value; }
        }
        //店仓编号
        public string customerCode;
        public string CUSTOMERCODE
        {
            get { return customerCode; }
            set { customerCode = value; }
        }
        //收货人姓名
        public string shr;
        public string SHR
        {
            get { return shr; }
            set { shr = value; }
        }
        //手机号
        public string phone;
        public string PHONE
        {
            get { return phone; }
            set { phone = value; }
        }
        //企微手机号
        public string mobile;
        public string MOBILE
        {
            get { return mobile; }
            set { mobile = value; }
        }
        //收货人地址
        public string address;
        public string ADDRESS
        {
            get { return address; }
            set { address = value; }
        }
        //备注
        public string remark;
        public string REMARK
        {
            get { return remark; }
            set { remark = value; }
        }

    }
    //尺码
    public class mx
    {
        //tmid
        public string tmid;
        public string TMID
        {
            get { return tmid; }
            set { tmid = value; }
        }

        //数量
        public int? sl;
        public int? SL
        {
            get { return sl; }
            set { sl = value; }
        }
        //备注
        public string mark;
        public string MARK
        {
            get { return mark; }
            set { mark = value; }
        }

        public string imageurl;
        public string IMAGEURL
        {
            get { return imageurl; }
            set { imageurl = value; }
        }

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}